package com.gzc.just.play.last.war.common.share.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

/**
 * All-in-One模式配置类
 * 根据环境自动选择数据源和缓存实现
 */
@Configuration
public class AllInOneConfig {
    
    private static final Logger logger = LoggerFactory.getLogger(AllInOneConfig.class);
    
    @Value("${spring.profiles.active:dev-all-in-one}")
    private String activeProfile;
    
    /**
     * 环境检测Bean
     */
    @Bean
    public EnvironmentDetector environmentDetector() {
        logger.info("Current active profile: {}", activeProfile);
        
        EnvironmentDetector detector = new EnvironmentDetector();
        detector.setActiveProfile(activeProfile);
        
        return detector;
    }
    
    /**
     * 开发环境配置检查
     */
    @Bean
    @Profile("dev-all-in-one")
    @ConditionalOnProperty(name = "all-in-one.enabled", havingValue = "true", matchIfMissing = true)
    public DevelopmentConfig developmentConfig() {
        logger.info("Initializing All-in-One development environment configuration");
        
        DevelopmentConfig config = new DevelopmentConfig();
        config.setDatabaseType("SQLite");
        config.setCacheType("Caffeine");
        config.setSingleProcessMode(true);
        
        return config;
    }
    
    /**
     * 生产环境配置检查
     */
    @Bean
    @Profile("prod")
    public ProductionConfig productionConfig() {
        logger.info("Initializing production environment configuration");
        
        ProductionConfig config = new ProductionConfig();
        config.setDatabaseType("MySQL");
        config.setCacheType("Redis");
        config.setSingleProcessMode(false);
        
        return config;
    }
    
    /**
     * 环境检测器
     */
    public static class EnvironmentDetector {
        
        private String activeProfile;
        
        public boolean isDevelopment() {
            return "dev-all-in-one".equals(activeProfile);
        }
        
        public boolean isProduction() {
            return "prod".equals(activeProfile);
        }
        
        public String getActiveProfile() {
            return activeProfile;
        }
        
        public void setActiveProfile(String activeProfile) {
            this.activeProfile = activeProfile;
        }
    }
    
    /**
     * 开发环境配置
     */
    public static class DevelopmentConfig {
        
        private String databaseType;
        private String cacheType;
        private boolean singleProcessMode;
        
        // Getters and Setters
        public String getDatabaseType() { return databaseType; }
        public void setDatabaseType(String databaseType) { this.databaseType = databaseType; }
        
        public String getCacheType() { return cacheType; }
        public void setCacheType(String cacheType) { this.cacheType = cacheType; }
        
        public boolean isSingleProcessMode() { return singleProcessMode; }
        public void setSingleProcessMode(boolean singleProcessMode) { this.singleProcessMode = singleProcessMode; }
    }
    
    /**
     * 生产环境配置
     */
    public static class ProductionConfig {
        
        private String databaseType;
        private String cacheType;
        private boolean singleProcessMode;
        
        // Getters and Setters
        public String getDatabaseType() { return databaseType; }
        public void setDatabaseType(String databaseType) { this.databaseType = databaseType; }
        
        public String getCacheType() { return cacheType; }
        public void setCacheType(String cacheType) { this.cacheType = cacheType; }
        
        public boolean isSingleProcessMode() { return singleProcessMode; }
        public void setSingleProcessMode(boolean singleProcessMode) { this.singleProcessMode = singleProcessMode; }
    }
}